import { service, crudParamsSerializer } from '@/utils/request'
import { CreateQueryParams } from '@nestjsx/crud-request'
import { Score } from './score.dto'

export const defaultScoreData: Partial<Score> = {}

export function getScores (params?: CreateQueryParams) {
  return service.get<Score[]>(
    '/resources/scores',
    {
      params,
      paramsSerializer: crudParamsSerializer
    }
  )
}

export function getScoreById (id: number | string, params?: CreateQueryParams) {
  return service.get<Score>(
    `/resources/scores/${id}`,
    {
      params,
      paramsSerializer: crudParamsSerializer
    }
  )
}

export function createScore (dto: Partial<Score>, params?: CreateQueryParams) {
  const data = new FormData()
  if (dto.tag) { data.append('tag', dto.tag) }
  if (dto.file instanceof Blob) { data.append('file', dto.file) }
  if (dto.uploadedByUser) { data.append('uploadedByUser', JSON.stringify(dto.uploadedByUser)) }
  if (dto.relatedSong) { data.append('relatedSong', JSON.stringify(dto.relatedSong)) }
  return service.post<Score>(
    '/resources/scores',
    data,
    {
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      params,
      paramsSerializer: crudParamsSerializer
    }
  )
}
